﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WHControlLib.CommonHelpers
{
public class ListCtrlPageManager
    {

        /// <summary>
        /// 获得分页后的总页数默认1页没有0页
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="ItemsList">数据列表</param>
        /// <param name="OnePageCount">每页有多少个元素</param>
        /// <returns>总页数</returns>
      public  int GetTotalPageCount <T>(List<T> ItemsList, int OnePageCount)
        { 
            int totalPageCount = 1;
            if (ItemsList.Count <= OnePageCount)
            {
                totalPageCount = 1;
            }
            else
                totalPageCount = ItemsList.Count % OnePageCount > 0 ? ItemsList.Count / OnePageCount + 1 : ItemsList.Count / OnePageCount;



            return totalPageCount;
        }

        /// <summary>
        /// 获得按 OnePageCount设定的分页数，NowPage指定当前页号的所有List单元
        /// </summary>
        /// <typeparam name="T">单元类型</typeparam>
        /// <param name="ItemsList">单元列表</param>
        /// <param name="OnePageCount">一页有多少个元素</param>
        /// <param name="NowPage">当前页号</param>
        /// <param name="Totalpag">共有多少页</param>
        /// <returns>当前页号下的元素单位列表</returns>

        public List<T> GetSplitedPageList<T>(List<T> ItemsList, int OnePageCount, int NowPage,out int Totalpag)


        {
            int ItemListCount = ItemsList.Count;
            List<T> DisPlayList = new List<T>();
            //int Totalpag;
            if (ItemsList.Count <= OnePageCount)
            {
                Totalpag = 1;
            }
            else
                Totalpag = ItemsList.Count % OnePageCount > 0 ? ItemsList.Count / OnePageCount + 1 : ItemsList.Count / OnePageCount;

            NowPage = NowPage < 1 ? 1 : NowPage;
            NowPage = NowPage > Totalpag ? Totalpag : NowPage;

            for (int i = 0; i < OnePageCount; i++)
            {

                int f = (NowPage - 1) * OnePageCount + i;

                if (f >= ItemsList.Count)
                {
                    break;
                }
                DisPlayList.Add(ItemsList[f]);

            }

            return DisPlayList;



        }

        //public List<Control> SetItemListToCtrlList<T>(List<T> SplitedPageList, Control ctrl, int OnepageCount)

        //{
        //    List<Control> DisPlayCtrlList = new List<Control>();
        //    for (int i = 0; i < SplitedPageList.Count; i++)
        //    {
        //        Control c = new Control();

        //    }


        //    return DisPlayCtrlList;

        //    }

        //}

        /// <summary>
        /// 将要显示的控件列表里的控件添加到其父控件中
        /// </summary>
        /// <param name="DisplayItemList">子控件列表</param>
        /// <param name="ParentCtrl">父控件</param>
        /// <param name="HCount">水平显示控件个数</param>
        /// <param name="VCount">垂直显示控件个数</param>
        /// <param name="OffsetXY">控件间上下偏移像素数</param>
      void  SetSplitedCtrlListToParent(List<Control> DisplayItemList, Control ParentCtrl, int HCount, int VCount, int OffsetXY)
        {

            ParentCtrl.Controls.Clear();

            int ItemCount = DisplayItemList.Count;
            int MaxCount = HCount * VCount;

            ItemCount = ItemCount > MaxCount ? MaxCount : ItemCount;

            int NeedVCount;


            NeedVCount = ItemCount / HCount;

            int f = 0;
            for (int j = 0; j <= NeedVCount; j++)
            {

                for (int i = 0; i < HCount; i++)
                {

                    f = i + j * HCount;
                    if (f >= ItemCount)
                    {
                        return;
                    }
                    DisplayItemList[f].Height = (int)ParentCtrl.Height / VCount - OffsetXY;
                    DisplayItemList[f].Width = (int)ParentCtrl.Width / HCount - OffsetXY;
                    DisplayItemList[f].Left = OffsetXY / 2 + DisplayItemList[f].Width * i;
                    DisplayItemList[f].Top = OffsetXY / 2 + j * DisplayItemList[f].Height;
                  

                    ParentCtrl.Controls.Add(DisplayItemList[f]);


                }

            }





        }
        /// <summary>
        /// 将控件列表转换为control类型的列表
        /// </summary>
        /// <typeparam name="T">子控件类型</typeparam>
        /// <param name="ChildCtrlClassList">子控件列表</param>
        /// <returns>Control类型控件列表</returns>
      public   List<Control> SetChildCtrClassListToCtrlList <T> (List<T>ChildCtrlClassList)
        {
            List<Control> CtrlList = new List<Control>();
            foreach (var item in ChildCtrlClassList)
            {
               Control c= item as Control;
                if (c!=null)
                {
                    CtrlList.Add(c);
                }
            }
            return CtrlList;

        }

        /// <summary>
        /// 增强将要显示的控件列表里的控件添加到其父控件中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ChildCtrlClassList">子控件对象列表</param>
        /// <param name="ParentCtrl">父控件对象</param>
        /// <param name="HCount">父控件水平显示行数</param>
        /// <param name="VCount">父控件垂直显示行数</param>
        /// <param name="OffsetXY">子控件间的上下和左右间隔像素</param>
        public void  SetSplitedCtrlListToParent<T>(List<T> ChildCtrlClassList, Control ParentCtrl, int HCount, int VCount, int OffsetXY)
        {


         List<Control> DisplayItemList=SetChildCtrClassListToCtrlList(ChildCtrlClassList);
            if (DisplayItemList==null)
            {
                return;
            }
            ParentCtrl.Controls.Clear();

            int ItemCount = DisplayItemList.Count;
            int MaxCount = HCount * VCount;

            ItemCount = ItemCount > MaxCount ? MaxCount : ItemCount;

            int NeedVCount;


            NeedVCount = ItemCount / HCount;

            int f = 0;
            for (int j = 0; j <= NeedVCount; j++)
            {

                for (int i = 0; i < HCount; i++)
                {

                    f = i + j * HCount;
                    if (f >= ItemCount)
                    {
                        return;
                    }
                    DisplayItemList[f].Height = (int)ParentCtrl.Height / VCount - OffsetXY;
                    DisplayItemList[f].Width = (int)ParentCtrl.Width / HCount - OffsetXY;


                   DisplayItemList[f].Left = OffsetXY / 2*(i+1) +  i*DisplayItemList[f].Width ;
                    DisplayItemList[f].Top = OffsetXY / 2*(j+1 ) + j * DisplayItemList[f].Height;
                    

                    ParentCtrl.Controls.Add(DisplayItemList[f]);


                }

            }





        }



        ////////////////////////////////////////////////////////////

    }
}
